package com.ruoyi.web.controller.system;

import java.io.BufferedOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.domain.server.Sys;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Upfile;
import com.ruoyi.system.service.IUpfileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户上传文件管理Controller
 * 
 * @author ruoyi
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/system/upfile")
public class UpfileController extends BaseController
{
    @Autowired
    private IUpfileService upfileService;

    /**
     * 查询用户上传文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:upfile:list')")
    @GetMapping("/list")
    public TableDataInfo list(Upfile upfile)
    {
        startPage();
        List<Upfile> list = upfileService.selectUpfileList(upfile);
        return getDataTable(list);
    }

    /**
     * 导出用户上传文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:upfile:export')")

    @PostMapping("/export")
    public void export(HttpServletResponse response, Upfile upfile)
    {
        List<Upfile> list = upfileService.selectUpfileList(upfile);
        ExcelUtil<Upfile> util = new ExcelUtil<Upfile>(Upfile.class);
        util.exportExcel(response, list, "用户上传文件管理数据");
    }

    /**
     * 获取用户上传文件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:upfile:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(upfileService.selectUpfileByFileId(fileId));
    }

    /**
     * 新增用户上传文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:upfile:add')")

    @PostMapping
    public AjaxResult add(@RequestBody Upfile upfile)
    {

        byte[] bytes = java.util.Base64.getDecoder().decode(upfile.getFile());
        // 将字节数组转换为字符串
        upfile.setFileSize((long) bytes.length);
        upfile.setFileType("application/pdf");
        return toAjax(upfileService.insertUpfile(upfile));
    }

    /**
     * 修改用户上传文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:upfile:edit')")

    @PutMapping
    public AjaxResult edit(@RequestBody Upfile upfile)
    {
        return toAjax(upfileService.updateUpfile(upfile));
    }

    /**
     * 删除用户上传文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:upfile:remove')")

	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(upfileService.deleteUpfileByFileIds(fileIds));
    }
}
